package com.kin207.netty.message.mqtt;


import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.kin207.netty.message.mqtt.service.LockMqttService;

@Component
public class MqttMessageCallbackListener implements MqttCallback {
	final static Logger logger = LoggerFactory.getLogger(MqttMessageCallbackListener.class);
	private LockMqttService lockMqttService;
	public void connectionLost(Throwable cause) {
		// TODO Auto-generated method stub
	}
    public MqttMessageCallbackListener() {}

    public MqttMessageCallbackListener(LockMqttService lockMqttService) {
        this.lockMqttService = lockMqttService;
    }
	/**
	 * MQTT鐩戝惉鏁版嵁
	 * 
	 */
	public void messageArrived(String topic, MqttMessage message) throws Exception {
		String mssageMsg = new String(message.getPayload());
		JSONObject msg = JSONObject.parseObject(mssageMsg);
		logger.debug("Topic+message:"+topic+";"+msg.get("sid"));
		try {
			logger.debug("Topic+message:"+lockMqttService);
			lockMqttService.unlock(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void deliveryComplete(IMqttDeliveryToken token) {
		// TODO Auto-generated method stub
		logger.debug("Topic+message:token"+"Q.MSG.NETTY.PING");
	}


}